package cc.rboot.feign.transcoder;

import com.alibaba.fastjson.JSONObject;
import feign.RequestTemplate;
import feign.codec.EncodeException;
import feign.codec.Encoder;
import jodd.net.HttpMethod;

import java.lang.reflect.Type;
import java.util.Map;

public class FeignEncoder implements Encoder {

    @Override
    public void encode(Object o, Type type, RequestTemplate requestTemplate) throws EncodeException {
        switch (requestTemplate.method()) {
            case "GET": {
                Map<String, ?> map = (Map<String, ?>) o;
                for (String key : map.keySet()) {
                    requestTemplate.query(key, String.valueOf(map.get(key)));
                }
                break;
            }
            case "POST" : {
                requestTemplate.body(JSONObject.toJSONString(o));
                break;
            }
            default: {
                requestTemplate.body(JSONObject.toJSONString(o));
            }
        }
    }
}
